#pragma once

#include<set>

#include"ControlerBase.h"

using namespace std;

class Controlers
{
	friend ControlerBase* GetControler(Unit* whichUnit);
public:
	static void OnFrame()
	{
		set<ControlerBase*> controlersToRemove;
		for each(ControlerBase* whichControler in _controlers)
		{
			whichControler->OnFrame();
			if(whichControler->IsDead())
			{
				controlersToRemove.insert(whichControler);
			}
		}
		for each(ControlerBase* whichToRemove in controlersToRemove)
		{
			delete whichToRemove;
			_controlers.erase(whichToRemove);
		}
	}
	static void Add(ControlerBase* controler)
	{
		_controlers.insert(controler);
		_map[controler->GetControllingUnit()] = controler;
	}

private:
	static set<ControlerBase*> _controlers;
	static map<Unit* , ControlerBase*> _map;
};

set<ControlerBase*> Controlers::_controlers;
map<Unit* , ControlerBase*> Controlers::_map;

ControlerBase* GetControler(Unit* whichUnit)
{
	return Controlers::_map[whichUnit];
}